package com.qiwenshare.file.controller;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8081);
            System.out.println("服务器已启动，等待客户端连接...");

            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接");

            InputStream inputStream = socket.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\swq\\release\\static\\upload\\23231121.jpg");

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }

            fileOutputStream.close();
            bufferedInputStream.close();
            inputStream.close();
            socket.close();
            serverSocket.close();

            System.out.println("文件传输完成");
        } catch (IOException e) {
            e.printStackTrace();
        }

      /*  try (Socket socket = new Socket("127.0.0.1", 8081);
             FileInputStream  fileInputStream = new FileInputStream("D:\\swq\\release\\static\\upload\\23231121.jpg");
             OutputStream outputStream = socket.getOutputStream();) {
            byte[] bytes = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }


}
